
package com.android.fitnesscenter.view.client;

import java.text.DecimalFormat;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ConfigProperties;
import com.android.fitnesscenter.utils.ConfigProperties.TradingType;
import com.android.fitnesscenter.utils.Loger;
import com.android.fitnesscenter.utils.MapEntity;
import com.android.fitnesscenter.utils.ToastHelper;
import com.android.fitnesscenter.utils.ViewHelper;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class ClientMainActivity extends ClientBaseActivity {

    @ViewInject(R.id.btn_top_up)
    public Button btn_top_up;

    @ViewInject(R.id.btn_pay)
    public Button btn_pay;

    @ViewInject(R.id.txt_find_detail)
    public TextView txt_find_detail;

    @ViewInject(R.id.txt_money)
    public TextView txt_money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client_main_layout);
        ViewUtils.inject(this);
        initTitleBar(getString(R.string.txt_operate), true);
        ibtn_back.setBackgroundResource(R.drawable.icon_logout);
        ibtn_more.setBackgroundResource(R.drawable.icon_setting);
        ibtn_more.setVisibility(View.VISIBLE);
        String token = mSharePrefsHelper.getString("token", "");
        if (!TextUtils.isEmpty(token)) {
            myAccount(token);
        }
        isCheckVersion = true;
        setWifiDialog();
    }

    @Override
    protected void ibtn_back() {
        DialogInterface.OnClickListener positiveListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                logout();
            }
        };
        new ViewHelper(ClientMainActivity.this).showBTN2Dialog("提示", "是否退出此账号?", "确定", "取消", positiveListener,
                null, null);
    }

    @OnClick(R.id.btn_top_up)
    public void btn_top_up(View view) {
        queryNewInfo(FROM_CLIENT_MAIN_TOP_UP_ACTIVITY, TradingType.TOP_UP);
    }

    @Override
    protected void parseQueryNewInfoResult(int fromActivity, MapEntity entity) {
        if (fromActivity == FROM_CLIENT_MAIN_TOP_UP_ACTIVITY) {
            if (entity != null && !entity.isEmpty()) {
                Intent intent = new Intent(ClientMainActivity.this, ClientTopUpActivity.class);
                startActivityForResult(intent, 0x0001);
            } else {
                ToastHelper.getInstance().showShortMsg("暂无充值记录数据可确认!");
            }
        } else if (fromActivity == FROM_CLIENT_MAIN_PAY_ACTIVITY) {
            if (entity != null && !entity.isEmpty()) {
                Intent intent = new Intent(ClientMainActivity.this, ClientPayActivity.class);
                startActivityForResult(intent, 0x0002);
            } else {
                ToastHelper.getInstance().showShortMsg("暂无消费记录数据可确认!");
            }
        }
    }

    @OnClick(R.id.btn_pay)
    public void btn_pay(View view) {
        queryNewInfo(FROM_CLIENT_MAIN_PAY_ACTIVITY, TradingType.PAY);
    }

    @OnClick(R.id.txt_find_detail)
    public void txt_find_detail(View view) {
        Intent intent = new Intent(ClientMainActivity.this, ClientDetailActivity.class);
        startActivity(intent);
    }

    private void myAccount(String token) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        String imei = getIMEI();
        params.addBodyParameter("token", token);
        params.addBodyParameter("imei", imei);

        String url = ConfigProperties.SERVICE_URL + "myAccount.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                if (mDialog == null) {
                    mDialog = new ProgressDialog(ClientMainActivity.this);
                }
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("查询账户信息中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (ClientMainActivity.this != null && !ClientMainActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    String result = jsonObject.getString("result");
                                    if (!TextUtils.isEmpty(result) && result.contains(".")) {
                                        DecimalFormat df = new DecimalFormat("##0.00");
                                        double f = Double.parseDouble(result);
                                        result = df.format(f);
                                    } else if (TextUtils.isEmpty(result)) {
                                        result = "0";
                                    }
                                    txt_money.setText(result);
                                    String minusLimit = jsonObject.getString("minusLimit");
                                    mSharePrefsHelper.setString("minusLimit", minusLimit);
                                } else {
                                    String msg = jsonObject.getString("message");
                                    if (TextUtils.equals(msg, "查询用户帐户出错")) {
                                        txt_money.setText("0");
                                    } else {
                                        new ViewHelper(ClientMainActivity.this).showBTNDialog(msg);
                                    }
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(ClientMainActivity.this).showBTNDialog("账户信息查找失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                if (ClientMainActivity.this != null && !ClientMainActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    Loger.e("onFailure msg is " + msg);
                    new ViewHelper(ClientMainActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0x0001) {
            String token = mSharePrefsHelper.getString("token", "");
            if (!TextUtils.isEmpty(token)) {
                myAccount(token);
            }
        }
    }

    @Override
    protected void titlebarMethod(int id) {
        switch (id) {
            case R.id.ibtn_more:
                Intent intent = new Intent(ClientMainActivity.this, ClientSettingActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

}
